#!/usr/bin/perl
#lvformat.cgi
require './lvm-lib.pl';
&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);
my ($lvdev, $voldir, @lvinfo, $l, $lv);
$config{'mkfs_lock_new'} = "/var/nas/lock/mkfsd_new";
$err_msg="";
$log_ev="";

$lvdev = $in{'lvdev'};
$voldir = $in{'voldir'};
$fstype = $in{'type'};
$nc =$in{'nc'};
$pass1 = $in{'passwd'};
$pass2 = $in{'passwd1'};
if($nc eq "y"){
	if($pass1 ne $pass2){
		$err_msg="$text{'err_diffpass'}";
		goto ERROR_OUT;
	}
	if(!($pass1 =~ /^[a-zA-Z0-9]{6,}$/)){
		$err_msg= $text{'fspassrule'};
		goto ERROR_OUT;
	}
	$lvdev =~ /(volume\d+)/;
	$cryptname=$1;
	`echo $pass1|cryptsetup create $cryptname $lvdev`;
	`date >> /sysvol/.cs`;
	$txt = $pass1.$lvdev;
	`echo $txt >> /sysvol/.cs`;
	$lvdev="/dev/mapper/".$cryptname;	
}
$err_msg = $lvdev."|".$voldir."|".$fstype."|".$nc."|$pass1|$pass2";
#goto ERROR_OUT;
#begin mkfs. Because mkfs is very time consumed, fork a child process

my $child_pid=fork;
if (!defined($child_pid)) {
        $log_ev = $text{'log_ev_mkfs_fork_err'};
        $log_msg= &text('log_msg_mkfs_fork_err', $volname);
        nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

        $err_msg = $text{'err_systemerror'};
        goto ERROR_OUT;
}

if ($child_pid) {

        open(MKFS_LOCK, ">".$config{'mkfs_lock_new'});
        print MKFS_LOCK $child_pid, "\n";
        close(MKFS_LOCK);
        &redirect("volstat.cgi");
}
else {
        #child process
        while(1) {
                if (-s $config{'mkfs_lock_new'}) {
                        last;
                }
                sleep(1);
        }
        exec './mkfsd.pl', $lvdev, $voldir, $fstype, $nc, $pass1;
}

ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);


